<template>
	<main-content
		:tableHeight.sync="tableHeight"
		:otherDependenComponents="otherDependenComponents"
	>
		<template slot="query">
			<el-form
				ref="formRef"
				:inline="true"
				:model="queryData"
				class="flex-warp-container"
			>
				<!-- <el-form-item
					:label="$t('common.belong_merchant') + ':'"
					class="flex-row-container"
					label-width="77px"
				>
					<merchant-select
						v-model="queryData.merchantId"
						:merchantInfo.sync="merchantObj"
					></merchant-select>
				</el-form-item> -->
				<el-form-item
					label="日报期数:"
					class="flex-row-container"
					label-width="77px"
				>
					<el-date-picker
						v-model="searchTime"
						type="daterange"
						:default-time="defaultTime"
						range-separator="-"
						:start-placeholder="$t('report.start_date')"
						:end-placeholder="$t('report.end_date')"
						:picker-options="pickerShortcutWithCurrPeriod"
						:clearable="false"
					></el-date-picker>
				</el-form-item>
				<el-form-item>
					<el-button
						type="primary"
						icon="el-icon-search"
						size="medium"
						@click="search"
					>
						{{ $t('common.search') }}
					</el-button>
					<el-button
						icon="el-icon-refresh-left"
						size="medium"
						@click="reset"
					>
						{{ $t('common.reset') }}
					</el-button>
					<el-button
						v-if="hasPermission('90330049')"
						type="warning"
						icon="el-icon-download"
						size="medium"
						:loading="exportBtnLoading"
						@click="handleExportExcel"
					>
						{{ $t('common.export') }}
					</el-button>
					<el-button
						type="success"
						icon="el-icon-sort"
						:disabled="loading"
						size="medium"
						@click="openColSettings"
					>
						{{ $t('common.row_set') }}
					</el-button>
				</el-form-item>
				<date-picker-shortcuts
					kind="shortcutForButtons"
					:action="handleShortcutBtnClick"
					:loading="loading"
				/>
			</el-form>
			<el-link :underline="false" type="danger">
				{{ $t('report.refresh_warning_text') }}
			</el-link>
		</template>
		<template slot="content">
			<el-table
				key="father"
				v-loading="loading"
				element-loading-spinner="el-icon-loading"
				border
				size="mini"
				class="small-size-table"
				:data="tableData"
				style="width: 100%"
				:max-height="tableHeight"
				:header-cell-style="getRowClass"
				highlight-current-row
			>
				<el-table-column
					prop="dataType"
					align="center"
					label="类别"
					show-overflow-tooltip
				>
					<!-- 1-官方，2-代理，3总计 -->
					<template slot-scope="scope">
						<span v-if="scope.row.dataType == 1">官方</span>
						<span v-else-if="scope.row.dataType == 2">代理</span>
						<span v-else-if="scope.row.dataType == 3">总计</span>
						<span v-else>-</span>
					</template>
				</el-table-column>
				<el-table-column
					v-if="tableColumnData['授信额度(万)']"
					prop="creditQuota"
					align="center"
					label="授信额度(万)"
					show-overflow-tooltip
					min-width="150"
				>
					<template slot-scope="scope">
						<p>
							{{
								handleTotalNumber('10k', scope.row.creditQuota)
							}}
						</p>
					</template>
				</el-table-column>
				<el-table-column
					v-if="tableColumnData['新增授信额度(万)']"
					prop="newCreditQuota"
					align="center"
					label="新增授信额度(万)"
					min-width="150"
					show-overflow-tooltip
				>
					<template slot-scope="scope">
						<p>
							{{
								handleTotalNumber(
									'10k',
									scope.row.newCreditQuota
								)
							}}
						</p>
					</template>
				</el-table-column>
				<el-table-column
					v-if="tableColumnData['充值金额(万)']"
					prop="depositAmount"
					align="center"
					min-width="150"
					label="充值金额(万)"
					show-overflow-tooltip
				>
					<template slot-scope="scope">
						<div
							class="text-link"
							@click="dialogData(scope.row, 'payIn')"
						>
							{{
								handleTotalNumber(
									'10k',
									scope.row.depositAmount
								)
							}}
						</div>
					</template>
				</el-table-column>
				<el-table-column
					v-if="tableColumnData['提款金额(万)']"
					prop="withdrawAmount"
					align="center"
					label="提款金额(万)"
					show-overflow-tooltip
					min-width="150"
				>
					<template slot-scope="scope">
						<div
							class="text-link"
							@click="dialogData(scope.row, 'payOut')"
						>
							{{
								handleTotalNumber(
									'10k',
									scope.row.withdrawAmount
								)
							}}
						</div>
					</template>
				</el-table-column>
				<el-table-column
					v-if="tableColumnData['总借款额度(万)']"
					prop="loanAmount"
					align="center"
					label="总借款额度(万)"
					show-overflow-tooltip
					min-width="150"
				>
					<template slot-scope="scope">
						<p>
							{{ handleTotalNumber('10k', scope.row.loanAmount) }}
						</p>
					</template>
				</el-table-column>
				<el-table-column
					v-if="tableColumnData['总还款额度(万)']"
					prop="totalRepayQuota"
					align="center"
					label="总还款额度(万)"
					show-overflow-tooltip
					min-width="150"
				>
					<template slot-scope="scope">
						<p>
							{{
								handleTotalNumber(
									'10k',
									scope.row.totalRepayQuota
								)
							}}
						</p>
					</template>
				</el-table-column>
				<el-table-column
					v-if="tableColumnData['新增借款额度(万)']"
					prop="newLoanAmount"
					align="center"
					label="新增借款额度(万)"
					show-overflow-tooltip
					min-width="150"
				>
					<template slot-scope="scope">
						<p>
							{{
								handleTotalNumber(
									'10k',
									scope.row.newLoanAmount
								)
							}}
						</p>
					</template>
				</el-table-column>
				<el-table-column
					v-if="tableColumnData['新增还款额度(万)']"
					prop="newRepayQuota"
					align="center"
					label="新增还款额度(万)"
					show-overflow-tooltip
					min-width="150"
				>
					<template slot-scope="scope">
						<p>
							{{
								handleTotalNumber(
									'10k',
									scope.row.newRepayQuota
								)
							}}
						</p>
					</template>
				</el-table-column>
				<el-table-column
					v-if="tableColumnData['应收账款(万)']"
					prop="shouldRepaymentAmount"
					align="center"
					label="应收账款(万)"
					show-overflow-tooltip
					min-width="150"
				>
					<template slot-scope="scope">
						<p>
							{{
								handleTotalNumber(
									'10k',
									scope.row.shouldRepaymentAmount
								)
							}}
						</p>
					</template>
				</el-table-column>
				<el-table-column
					v-if="tableColumnData['应付账款(万)']"
					prop="shouldPaymentAmount"
					align="center"
					label="应付账款(万)"
					show-overflow-tooltip
					min-width="150"
				>
					<template slot-scope="scope">
						<p>
							{{
								handleTotalNumber(
									'10k',
									scope.row.shouldPaymentAmount
								)
							}}
						</p>
					</template>
				</el-table-column>
				<el-table-column
					v-if="tableColumnData['新增有效用户(个)']"
					prop="newEffectiveNum"
					align="center"
					label="新增有效用户(个)"
					show-overflow-tooltip
					min-width="150"
				>
					<template slot-scope="scope">
						<p>
							{{ scope.row.newEffectiveNum || '-' }}
						</p>
					</template>
				</el-table-column>
				<el-table-column
					v-if="tableColumnData['活跃用户(个)']"
					prop="activeNum"
					align="center"
					label="活跃用户(个)"
					show-overflow-tooltip
					min-width="150"
				>
					<template slot-scope="scope">
						<p>
							{{ scope.row.activeNum || '-' }}
						</p>
					</template>
				</el-table-column>
				<el-table-column
					v-if="tableColumnData['总有效投注额(万)']"
					prop="validBetAmount"
					align="center"
					label="总有效投注额(万)"
					show-overflow-tooltip
					min-width="150"
				>
					<template slot-scope="scope">
						<p>
							{{
								handleTotalNumber(
									'10k',
									scope.row.validBetAmount
								)
							}}
						</p>
					</template>
				</el-table-column>
				<el-table-column
					v-if="tableColumnData['场馆输赢(万)']"
					prop="netAmount"
					align="center"
					label="场馆输赢(万)"
					show-overflow-tooltip
					min-width="150"
				>
					<template slot-scope="scope">
						<p :style="handleNumberColor(scope.row.netAmount)">
							{{
								handleTotalNumber(
									'10k',
									scope.row.netAmount,
									0,
									true,
									true
								)
							}}
						</p>
					</template>
				</el-table-column>
				<el-table-column
					v-if="tableColumnData['会员返水(万)']"
					prop="memberRebateAmount"
					align="center"
					label="会员返水(万)"
					show-overflow-tooltip
					min-width="150"
				>
					<template slot-scope="scope">
						<p>
							{{
								handleTotalNumber(
									'10k',
									scope.row.memberRebateAmount
								)
							}}
						</p>
					</template>
				</el-table-column>
				<el-table-column
					v-if="tableColumnData['代理返点(万)']"
					prop="proxyRebateAmount"
					align="center"
					label="代理返点(万)"
					show-overflow-tooltip
					min-width="150"
				>
					<template slot-scope="scope">
						<p>
							{{
								handleTotalNumber(
									'10k',
									scope.row.proxyRebateAmount
								)
							}}
						</p>
					</template>
				</el-table-column>
				<el-table-column
					v-if="tableColumnData['平台收入(万)']"
					prop="platformIncomeAmount"
					align="center"
					label="平台收入(万)"
					show-overflow-tooltip
					min-width="150"
				>
					<template slot-scope="scope">
						<p
							:style="
								handleNumberColor(
									scope.row.platformIncomeAmount
								)
							"
						>
							{{
								handleTotalNumber(
									'10k',
									scope.row.platformIncomeAmount,
									0,
									true,
									true
								)
							}}
						</p>
					</template>
				</el-table-column>
			</el-table>
			<!-- 分页 -->
			<!-- <el-pagination
				v-show="!!total"
				class="pageValue"
				:current-page.sync="pageNum"
				background
				layout="total, sizes,prev, pager, next, jumper"
				:page-size.sync="pageSize"
				:page-sizes="pageSizes"
				:total="total"
				:pager-count="9"
				@current-change="handleCurrentChange"
				@size-change="handleSizeChange"
			></el-pagination> -->
		</template>
		<detail-page
			v-if="showDialog"
			:rowVal="rowVal"
			:closeDialog="
				(val) => {
					showDialog = val
				}
			"
		/>
		<el-dialog
			:title="$t('common.row_set')"
			center
			:visible.sync="colSettings.visible"
			width="475px"
			class="setting-div"
		>
			<div>
				<el-link type="primary" class="mb15" @click="clickDel">
					复原缺省
				</el-link>
			</div>
			<div
				v-for="(value, name1) in activityListField"
				:key="name1"
				class="setting-div"
			>
				<el-checkbox v-model="dialogColumnData[name1]">
					{{ name1 }}
				</el-checkbox>
			</div>
			<div slot="footer" class="dialog-footer">
				<el-button @click="colSettings.visible = false">
					{{ $t('common.cancel') }}
				</el-button>
				<el-button type="primary" @click="handleOrderList">
					{{ $t('common.submit') }}
				</el-button>
			</div>
		</el-dialog>
	</main-content>
</template>

<script>
import list from '@/mixins/list'
import dayjs from 'dayjs'
import { routerNames } from '@/utils/consts'
import summaryList from './components/summaryList'
import MainContent from '@/components/MainContent/index.vue'
import DatePickerShortcuts from '@/components/DatePickerShortcuts'
const setSearchTime = () => [
	dayjs()
		.subtract(0, 'day')
		.startOf('day'),
	dayjs()
		.subtract(0, 'day')
		.endOf('day')
]
export default {
	name: routerNames.dataDailyReport,
	components: {
		DetailPage: () => import('./components/detail-page.vue'),
		MainContent,
		DatePickerShortcuts
	},
	mixins: [list, summaryList],
	data() {
		return {
			tableHeight: 480,
			otherDependenComponents: [],
			queryData: {
				payoutStatus: undefined,
				merchantId: ''
			},
			searchTime: setSearchTime(),
			// accountFlag: 'proxyName',
			tableData: [],
			subSummary: {},
			totalSummary: {},
			exportBtnLoading: false,
			merchantObj: '',
			axiosDate: '',
			showDialog: false,
			loading: false,
			colSettings: {
				visible: false,
				dialogColumnData: {}
			},
			merchantId: '',
			activityListField: {
				'授信额度(万)': true,
				'新增授信额度(万)': true,
				'充值金额(万)': true,
				'提款金额(万)': true,
				'总借款额度(万)': true,
				'总还款额度(万)': true,
				'新增借款额度(万)': true,
				'新增还款额度(万)': true,
				'应收账款(万)': true,
				'应付账款(万)': true,
				'新增有效用户(个)': true,
				'活跃用户(个)': true,
				'总有效投注额(万)': true,
				'场馆输赢(万)': true,
				'会员返水(万)': true,
				'代理返点(万)': true,
				'平台收入(万)': true
			}
		}
	},
	computed: {
		rebateArrList() {
			return this.globalDics.rebateArrList || []
		}
	},
	watch: {
		// accountFlag: {
		// 	handler(newVal, oldVal) {
		// 		this.$set(this.queryData, newVal, '')
		// 		this.$set(this.queryData, oldVal, '')
		// 	},
		// 	immediate: true
		// }
	},
	created() {
		this.$store.dispatch('user/updateCurrentAccountPeriod')
		this.initIndexDB('rebatePeriodReport', this.activityListField)
	},
	mounted() {},
	methods: {
		handleShortcutBtnClick(val) {
			this.searchTime = val
			this.pageNum = 1
			this.loadData()
		},
		// 提交列设置数据
		handleOrderList() {
			this.colSettings.visible = false
			this.updateIndexDB()
		},
		// 打开列设置弹框
		openColSettings() {
			this.getIndexDB(this.activityListField)
			this.colSettings.visible = true
		},
		clickDel() {
			this.dialogColumnData = {}
			this.dialogColumnData = {
				'授信额度(万)': true,
				'新增授信额度(万)': true,
				'充值金额(万)': true,
				'提款金额(万)': true,
				'总借款额度(万)': true,
				'总还款额度(万)': true,
				'新增借款额度(万)': true,
				'新增还款额度(万)': true,
				'应收账款(万)': true,
				'应付账款(万)': true,
				'新增有效用户(个)': true,
				'活跃用户(个)': true,
				'总有效投注额(万)': true,
				'场馆输赢(万)': true,
				'会员返水(万)': true,
				'代理返点(万)': true,
				'平台收入(万)': true
			}
		},
		dialogData(val, payType) {
			const timeArr = this.searchTime || []
			this.rowVal = {
				dataType: val.dataType,
				// merchantId: this.merchantId,
				payType: payType,
				startDate: dayjs(timeArr[0]).format('YYYYMMDD'),
				endDate: dayjs(timeArr[1]).format('YYYYMMDD')
			}
			this.showDialog = true
		},
		loadData() {
			if (+new Date() - 500 < this.axiosDate) return
			const timeArr = this.searchTime || []
			// if (!this.$store.getters.isZk) {
			//	this.queryData.merchantId = this.$store.getters.merchantInfo.id
			// }
			// else {
			// 	this.queryData.merchantId = this.queryData.merchantId
			// 		? this.queryData.merchantId
			// 		: this.$store.getters.merchantList[0].id
			// }
			const params = {
				...this.queryData,
				startDate: dayjs(timeArr[0]).format('YYYYMMDD'),
				endDate: dayjs(timeArr[1]).format('YYYYMMDD'),
				...this.getParams()
			}
			this.tableData = []
			this.loading = true
			// this.merchantId = undefined
			this.pageCurrency = this.merchantObj?.currency || ''
			this.$api
				.getDataDailyReportPage(params)
				.then((res) => {
					this.loading = false
					this.axiosDate = +new Date()
					if (res.code === 200) {
						// this.merchantId = this.queryData.merchantId
						this.tableData = res?.data || []
						// this.total = res?.data?.totalRecord || 0
						// this.subSummary = res?.data?.summary || {}
					}
				})
				.catch(() => {
					this.loading = false
				})
			// this.$api
			// 	.getDataDailyReportSummary(params)
			// 	.then((res) => {
			// 		this.loading = false
			// 		if (res.code === 200) {
			// 			this.totalSummary = res?.data || {}
			// 		}
			// 	})
			// 	.catch(() => {
			// 		this.loading = false
			// 	})
		},
		reset() {
			this.pageNum = 1
			this.queryData = {}
			this.merchantObj = ''
			this.searchTime = setSearchTime()
			this.loadData()
		},
		handleExportExcel() {
			const timeArr = this.searchTime || []
			const params = {
				...this.queryData,
				startDate: dayjs(timeArr[0]).format('YYYYMMDD'),
				endDate: dayjs(timeArr[1]).format('YYYYMMDD')
			}
			this.exportBtnLoading = true
			this.exportExcelPublic({
				api: 'getDataDailyReportExport',
				params
			}).finally(() => {
				this.exportBtnLoading = false
			})
		}
	}
}
</script>

<style lang="scss" scoped>
.tips-box {
	position: absolute;
	right: 10px;
	top: 50%;
	margin-top: -12px;
	font-size: 24px;
}

::v-deep .el-dialog__header {
	color: #909399;
	font-weight: 700;
}

::v-deep .el-table__fixed-footer-wrapper .cell,
::v-deep .el-table__footer-wrapper .cell {
	height: 100%;
	padding: 0 !important;
	margin: 0 !important;

	> div {
		height: 100%;
	}
}

::v-deep .footer_count_row {
	min-height: 50px;
	height: 50%;
	display: flex;
	font-weight: 700;
	padding: 5px 0;
	align-items: center;
	flex-direction: column;
	justify-content: center;
}

::v-deep .footer_count_row_border {
	border-top: 1px solid #ebeef5;
}
</style>
